package com.centaline.web.demo.controller;

import com.aist.common.utils.SpringUtils;
import com.aist.uam.auth.remote.UamSessionService;
import com.aist.uam.auth.remote.vo.SessionUser;
import com.aist.uam.permission.remote.UamPermissionService;
import com.aist.uam.permission.remote.vo.Menu;
import com.centaline.demo.entity.ToCase;
import com.centaline.demo.service.ToCaseService;
import com.centaline.httpinvoke.apiFaced.TeleiiFaced;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by caoyuan7 on 2018/1/29.
 */
@Controller
@RequestMapping("/")
public class ApplyController {

    @Autowired(required = true)
    ToCaseService toCaseService;

    @Autowired(required = true)
    UamSessionService uamSessionService;

    @Autowired(required = true)
    TeleiiFaced teleiiFaced;



    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("userRealName", uamSessionService.getSessionUser().getRealName());
        model.addAttribute("depart", uamSessionService.getSessionUser().getServiceCompanyName());
        //得到菜单
        SessionUser sessionUser = uamSessionService.getSessionUser();
        String jobId = sessionUser.getServiceJobId();
        String jobName = sessionUser.getServiceJobName();
        model.addAttribute("jobName", jobName);
        String code = "APPLY";
        Menu menu = SpringUtils.getBean(UamPermissionService.class).getMenuByJobAndMenuCode(jobId, code);
        model.addAttribute("menu", menu);
        return "index";
    }

    @RequestMapping("/permission")
    public String permission(Model model){
        return "index";
    }

    /**
     * httpinvoke访问mobile-apply-api-web中的方法，该方法利用httpClient访问接口：http://localhost:8080/demo
     * 该接口返回json字符串{"userSex":"man","userName":"jay"}
     */
    @RequestMapping("/apiDemo")
    public String apiDemo(Model model){
        try {
            String str = teleiiFaced.getMyUserMsg();
            model.addAttribute("str",str);
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "demo";
    }

    @RequestMapping("demo")
    @ResponseBody
    public Map demo() throws Exception {
        Map map = new HashMap<>();
        map.put("userName","jay");
        map.put("userSex","man");
        return map;
    }

    /**
     * 测试mybatis是否通
     * @author caoy
     * @return
     */
    @RequestMapping("/mybatisDemo")
    public String mybatisDemo(Model model){
        try {
            ToCase toCase = toCaseService.selectByPrimaryKey(1l);
            System.out.println(toCase.getCaseCode());
            model.addAttribute("str", toCase.getCaseCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "demo";
    }


}
